// 设置localStorage的值
const setLocal = (key, value) => {
    if (typeof window != 'undefined') {
        localStorage.setItem(key, JSON.stringify(value));
    }
};

// 获取localStorage的值
const getLocal = (key) => {
    if (typeof window != 'undefined') {
        const item = localStorage.getItem(key);
        return item ? JSON.parse(item) : null;
    }
};

// 移除localStorage的值
const removeLocal = (key) => {
    if (typeof window != 'undefined') {
        localStorage.removeItem(key);
    }
};

// 清除所有localStorage的值
const clearLocal = () => {
    if (typeof window != 'undefined') {
        localStorage.clear();
    }
};

// 设置sessionStorage的值
const setSession = (key, value) => {
    if (typeof window != 'undefined') {
        sessionStorage.setItem(key, JSON.stringify(value));
    }
};

// 获取sessionStorage的值
const getSession = (key) => {
    if (typeof window != 'undefined') {
        const item = sessionStorage.getItem(key);
        return item ? JSON.parse(item) : null;
    }
};

// 移除sessionStorage的值
const removeSession = (key) => {
    if (typeof window != 'undefined') {
        sessionStorage.removeItem(key);
    }
};

// 清除所有sessionStorage的值
const clearSession = () => {
    if (typeof window != 'undefined') {
        sessionStorage.clear();
    }
};

// 设置cookie的值
const setCookie = (name, value, hour) => {
    const date = new Date();
    date.setTime(date.getTime() + (hour * 60 * 60 * 1000));
    const expires = "expires=" + date.toUTCString();
    document.cookie = name + "=" + value + ";" + expires + ";path=/";
};

// 获取cookie的值
const getCookie = (name) => {
    const value = "; " + document.cookie;
    const parts = value.split("; " + name + "=");
    if (parts.length === 2) return parts.pop()?.split(";").shift();
};

// 移除cookie的值
const removeCookie = (name) => {
    setCookie(name, "", -1);
};

export  {
    setLocal,
    getLocal,
    removeLocal,
    clearLocal,
    setSession,
    getSession,
    removeSession,
    clearSession,
    setCookie,
    getCookie,
    removeCookie
};



